/**
 * 拼接字符表单 并提交
 * @param url
 * @param params
 * @returns {HTMLFormElement}
 */
function httpPost(url,params){
    var temp = document.createElement("form");
    temp.action = url;
    temp.method = "post";
    temp.style.display = "none";

    for(var p in params){
        var opt = document.createElement("textarea");
        opt.name = p;
        opt.value = params[p];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}


function jumpToAdd(){
    var params = {};
    httpPost("/articleController/jumpToAddView",params);
}

/**
 * 保存文章
 */
function saveArticle(){
    var articleTile = $("#title").val();
    var content = $("#content").val();
    var typeVal = $("#newType").val();
    var selectVal = $("#newTypeSelect").find("option:selected").val();
    /*var typeCode='';
    if(!!selectVal){
        typeCode = selectVal;
    }*/
    //说明：如果只有typeCode 说明选中的是下拉框 那就不用保存新类别
    //如果只有typeName  说明选中的输入框  就要保存新类别
    var params = {
        "articleTitle":articleTile,
        "articleContent":content,
        "typeId":selectVal,
        "typeName":typeVal
    };
    httpPost("/articleController/saveArticle",params);
}


function jumpToEditView(id){
    var params = {
        "articleId":id
    };
    httpPost("/articleController/jumpToEditView",params);
}

/**
 * 更新文章
 */
function updateArticle(){
    var articleId = $("#articleId").val();
    var articleTile = $("#title").val();
    var content = $("#content").val();
    var typeVal = $("#newType").val();
    var selectVal = $("#newTypeSelect").find("option:selected").val();
    var params = {
        "articleId":articleId,
        "articleTitle":articleTile,
        "articleContent":content,
        "typeId":selectVal,
        "typeName":typeVal
    };
    httpPost("/articleController/updateArticle",params);
}


/**
 * 进入管理界面
 */
function manageArticles(){
    var params = {};
    httpPost("/articleController/manageArticles",params);
}

//----------------------文章分类-----------------------
/**
 *
 */
function addNewArticleType(){
    $("#newType").show();
}

/**
 * 如果有值，下拉框就不可选
 */
function defineSelect(){
    var val = $("#newType").val();
    if(!!val){
        $("#newTypeSelect").attr("disabled",true);
        $("#newTypeSelect").val("");
    }else{
        $("#newTypeSelect").attr("disabled",false);
    }
}

/**
 * 如果有值，输入框就不可选
 */
function defineNewType(){
    var val = $("#newTypeSelect option:selected").val();
    if(!!val){
        $("#newType").val("");
        $("#newType").attr("disabled",true);
    }else{
        $("#newType").attr("disabled",false);
    }
}